home *** CD-ROM | disk | FTP | other *** search
/ Exame Informatica 139 / Exame Informatica 139.iso / Internet / NVU / chrome / toolkit.jar / content / mozapps / profile / profileSelection.js < prev    next >
Encoding:
Text File  |  2004-09-09  |  7.8 KB  |  258 lines

  1. /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code, released
  14.  * March 31, 1998.
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape
  17.  * Communications Corporation. Portions created by Netscape are
  18.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  19.  * Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Ben Goodger (03/01/00)
  23.  *   Seth Spitzer (28/10/99)
  24.  *   Dan Veditz <dveditz@netscape.com>
  25.  *   Benjamin Smedberg <bsmedberg@covad.net>
  26.  */
  27.  
  28. const C = Components.classes;
  29. const I = Components.interfaces;
  30.  
  31. const ToolkitProfileService = "@mozilla.org/toolkit/profile-service;1";
  32. const PromptService = "@mozilla.org/embedcomp/prompt-service;1";
  33.  
  34. var gDialogParams;
  35. var gProfileManagerBundle;
  36. var gBrandBundle;
  37. var gProfileService;
  38. var gPromptService;
  39.  
  40. function startup()
  41. {
  42.   try {
  43.     gDialogParams = window.arguments[0].
  44.       QueryInterface(I.nsIDialogParamBlock);
  45.  
  46.     gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService);
  47.  
  48.     gProfileManagerBundle = document.getElementById("bundle_profileManager");
  49.     gBrandBundle = document.getElementById("bundle_brand");
  50.  
  51.     gPromptService = C[PromptService].getService(I.nsIPromptService);
  52.  
  53.     document.documentElement.centerWindowOnScreen();
  54.  
  55.     var profilesElement = document.getElementById("profiles");
  56.  
  57.     var profileList = gProfileService.profiles;
  58.     while (profileList.hasMoreElements()) {
  59.       var profile = profileList.getNext().QueryInterface(I.nsIToolkitProfile);
  60.  
  61.       var listitem = profilesElement.appendItem(profile.name, "");
  62.  
  63.       var tooltiptext =
  64.         gProfileManagerBundle.getFormattedString("profileTooltip", [profile.name, profile.rootDir.path]);
  65.       listitem.setAttribute("tooltiptext", tooltiptext);
  66.       listitem.setAttribute("class", "listitem-iconic");
  67.       listitem.profile = profile;
  68.       try {
  69.         if (profile === gProfileService.selectedProfile) {
  70.           profilesElement.selectedItem = listitem;
  71.         }
  72.       }
  73.       catch(e) { }
  74.     }
  75.  
  76.     var autoSelectLastProfile = document.getElementById("autoSelectLastProfile");
  77.     autoSelectLastProfile.checked = gProfileService.startWithLastProfile;
  78.     profilesElement.focus();
  79.   }
  80.   catch(e) {
  81.     window.close();
  82.     throw (e);
  83.   }
  84. }
  85.  
  86. function acceptDialog()
  87. {
  88.   var appName = gBrandBundle.getString("brandShortName");
  89.  
  90.   var profilesElement = document.getElementById("profiles");
  91.   var selectedProfile = profilesElement.selectedItem;
  92.   if (!selectedProfile) {
  93.     var pleaseSelectTitle = gProfileManagerBundle.getString("pleaseSelectTitle");
  94.     var pleaseSelect =
  95.       gProfileManagerBundle.getFormattedString("pleaseSelect", [appName]);
  96.     gPromptService.alert(window, pleaseSelectTitle, pleaseSelect);
  97.  
  98.     return false;
  99.   }
  100.  
  101.   var profileLock;
  102.  
  103.   try {
  104.     profileLock = selectedProfile.profile.lock();
  105.   }
  106.   catch (e) {
  107.     var lockedTitle = gProfileManagerBundle.getString("profileLockedTitle");
  108.     var locked =
  109.       gProfileManagerBundle.getFormattedString("profileLocked", [appName, selectedProfile.profile.name]);
  110.     gPromptService.alert(window, lockedTitle, locked);
  111.  
  112.     return false;
  113.   }
  114.   gDialogParams.objects.insertElementAt(profileLock.nsIProfileLock, 0, false);
  115.  
  116.   var autoSelectLastProfile = document.getElementById("autoSelectLastProfile");
  117.   gProfileService.startWithLastProfile = autoSelectLastProfile.checked;
  118.   gProfileService.selectedProfile = selectedProfile.profile;
  119.  
  120.   gProfileService.startOffline = document.getElementById("offlineState").checked;
  121.  
  122.   gDialogParams.SetInt(0, 1);
  123.  
  124.   return true;
  125. }
  126.  
  127. // handle key event on listboxes
  128. function onProfilesKey(aEvent)
  129. {
  130.   switch( aEvent.keyCode ) 
  131.   {
  132.   case 46:
  133.     confirmDelete();
  134.     break;
  135.   case "VK_F2":
  136.     renameProfile();
  137.     break;
  138.   }
  139. }
  140.  
  141. function onProfilesDblClick(aEvent)
  142. {
  143.   if(aEvent.target.localName == "listitem")
  144.     document.documentElement.acceptDialog();
  145. }
  146.  
  147. // invoke the createProfile Wizard
  148. function CreateProfileWizard()
  149. {
  150.   window.openDialog('chrome://mozapps/content/profile/createProfileWizard.xul',
  151.                     '', 'centerscreen,chrome,modal,titlebar', gProfileService);
  152. }
  153.  
  154. /**
  155.  * Called from createProfileWizard to update the display.
  156.  */
  157. function CreateProfile(aProfile)
  158. {
  159.   var profilesElement = document.getElementById("profiles");
  160.  
  161.   var listitem = profilesElement.appendItem(aProfile.name, "");
  162.  
  163.   var tooltiptext =
  164.     gProfileManagerBundle.getFormattedString("profileTooltip", [aProfile.name, aProfile.rootDir.path]);
  165.   listitem.setAttribute("tooltiptext", tooltiptext);
  166.   listitem.setAttribute("class", "listitem-iconic");
  167.   listitem.profile = aProfile;
  168.  
  169.   profilesElement.ensureElementIsVisible(listitem);
  170.   profilesElement.selectItem(listitem);
  171. }
  172.  
  173. // rename the selected profile
  174. function RenameProfile()
  175. {
  176.   var profilesElement = document.getElementById("profiles");
  177.   var selectedItem = profilesElement.selectedItem;
  178.   if (!selectedItem) {
  179.     return false;
  180.   }
  181.  
  182.   var selectedProfile = selectedItem.profile;
  183.  
  184.   var oldName = selectedProfile.name;
  185.   var newName = {value: oldName};
  186.  
  187.   var dialogTitle = gProfileManagerBundle.getString("renameProfileTitle");
  188.   var msg =
  189.     gProfileManagerBundle.getFormattedString("renameProfilePrompt", [oldName]);
  190.  
  191.   if (gPromptService.prompt(window, dialogTitle, msg, newName, null, {value:0})) {
  192.     newName = newName.value;
  193.  
  194.     // User hasn't changed the profile name. Treat as if cancel was pressed.
  195.     if (newName == oldName)
  196.       return false;
  197.  
  198.     try {
  199.       selectedProfile.name = newName;
  200.     }
  201.     catch (e) {
  202.       var alTitle = gProfileManagerBundle.getString("profileNameInvalidTitle");
  203.       var alMsg = gProfileManagerBundle.getFormattedString("profileNameInvalid", [newName]);
  204.       gPromptService.alert(window, alTitle, alMsg);
  205.       return false;
  206.     }
  207.  
  208.     selectedItem.label = newName;
  209.     var tooltiptext =
  210.       gProfileManagerBundle.getFormattedString("profileTooltip", [newName, aProfile.rootDir.path]);
  211.     listitem.setAttribute("tooltiptext", tooltiptext);
  212.  
  213.     return true;
  214.   }
  215.  
  216.   return false;
  217. }
  218.  
  219. function ConfirmDelete()
  220. {
  221.   var deleteButton = document.getElementById("delbutton");
  222.   var profileList = document.getElementById( "profiles" );
  223.  
  224.   var selectedItem = profileList.selectedItem;
  225.   if (!selectedItem) {
  226.     return false;
  227.   }
  228.  
  229.   var selectedProfile = selectedItem.profile;
  230.   var deleteFiles = false;
  231.  
  232.   if (selectedProfile.rootDir.exists()) {
  233.     var dialogTitle = gProfileManagerBundle.getString("deleteTitle");
  234.     var dialogText =
  235.       gProfileManagerBundle.getFormattedString("deleteProfile",
  236.                                                [selectedProfile.rootDir.path]);
  237.  
  238.     var buttonPressed = gPromptService.confirmEx(window, dialogTitle, dialogText,
  239.                           (gPromptService.BUTTON_TITLE_IS_STRING * gPromptService.BUTTON_POS_0) +
  240.                           (gPromptService.BUTTON_TITLE_CANCEL * gPromptService.BUTTON_POS_1) +
  241.                           (gPromptService.BUTTON_TITLE_IS_STRING * gPromptService.BUTTON_POS_2),
  242.                           gProfileManagerBundle.getString("dontDeleteFiles"),
  243.                           null,
  244.                           gProfileManagerBundle.getString("deleteFiles"),
  245.                           null, {value:0});
  246.     if (buttonPressed == 1)
  247.       return false;
  248.  
  249.     if (buttonPressed == 2)
  250.       deleteFiles = true;
  251.   }
  252.   
  253.   selectedProfile.remove(deleteFiles);
  254.   selectedItem.parentNode.removeChild(selectedItem);
  255.  
  256.   return true;
  257. }
  258.